package com.qiwo.webshow.controller;


import java.util.Arrays;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qiwo.common.base.AdminBaseController;
import com.qiwo.webshow.domain.FeedbackDO;
import com.qiwo.webshow.service.FeedbackService;
import com.qiwo.common.utils.Result;

/**
 * 
 * <pre>
 * 意见反馈管理
 *  @author  chenwenchao
 *  @date 2018-10-25 17:28:10
 * </pre>
 * <small> 2018-10-25 17:28:10 | chenwenchao</small>
 */
@Controller
@RequestMapping("/webshow/feedback")
public class FeedbackController extends AdminBaseController {
	@Autowired
	private FeedbackService feedbackService;
	
	@GetMapping()
	@RequiresPermissions("webshow:feedback:feedback")
	String Feedback(){
	    return "webshow/feedback/feedback";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("webshow:feedback:feedback")
	public Result<Page<FeedbackDO>> list(FeedbackDO feedbackDTO){
        Wrapper<FeedbackDO> wrapper = new EntityWrapper<FeedbackDO>().orderBy("id", false);
        if(feedbackDTO!=null){
        	if(feedbackDTO.getSuggestType() == null || !feedbackDTO.getSuggestType().equals("") ){
        		wrapper.eq("suggest_type",feedbackDTO.getSuggestType());
			}
		}
        Page<FeedbackDO> page = feedbackService.selectPage(getPage(FeedbackDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("webshow:feedback:add")
	String add(){
	    return "webshow/feedback/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("webshow:feedback:edit")
	String edit(@PathVariable("id") Long id,Model model){
		FeedbackDO feedback = feedbackService.selectById(id);
		model.addAttribute("feedback", feedback);
	    return "webshow/feedback/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("webshow:feedback:add")
	public Result<String> save( FeedbackDO feedback){
		feedbackService.insert(feedback);
        return Result.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("webshow:feedback:edit")
	public Result<String>  update( FeedbackDO feedback){
		boolean update = feedbackService.updateById(feedback);
		return update ? Result.ok() : Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("webshow:feedback:remove")
	public Result<String>  remove( Long id){
		feedbackService.deleteById(id);
        return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("webshow:feedback:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		feedbackService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
